using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using System.Linq;

namespace Core.SimpleTemp.Api.ZB
{
    /// <summary>
    /// ZB_TrainPlan
    ///</summary>
    [ApiExplorerSettings(GroupName = "Sys")]//swagger分类标签
    [Route("api/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class ZB_TrainPlanController : CoreApiController
    {
        private readonly ZB_TrainPlanService _service;
        /// <summary>
        /// ZB_TrainPlan
        ///</summary>
        public ZB_TrainPlanController(ZB_TrainPlanService service)
        {
            _service = service;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList")]
        [PermissionFilter(nameof(ZB_TrainPlanController) + "_" + nameof(ZB_TrainPlanController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult>   GetList(PagingModel<ZB_TrainPlanDto>  pagingQueryModel)
        {
            IPageModel<ZB_TrainPlanDto>  result;
            result = await _service.GetList(pagingQueryModel);
            return this.JsonSuccess(result);
        }

        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(ZB_TrainPlanController) + "_" + nameof(ZB_TrainPlanController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            SaveAsync(ZB_TrainPlanDto dto)
        {
            //如果没初始化双总部员工当年培训分数，则设置
            _service.iniYearStudyNum();
           

            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                await _service.InsertAsync(dto);

                //在添加后初始化当年总部考试人员
                _service.iniZB_ExamUser();

                return this.JsonSuccess(string.Empty);
            }
            else
            {
                await _service.UpdateAsync(dto);

                //在添加后初始化当年总部考试人员
                System.Threading.Thread.Sleep(1 * 1000);
                _service.iniZB_ExamUser();


                return this.JsonSuccess(string.Empty);
            }
           
        }

        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(ZB_TrainPlanController) + "_" + nameof(ZB_TrainPlanController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult>
            DeleteMutiAsync([FromBody]Guid[] ids)
        {

            await _service.DeleteBatchAsync(ids);
            return this.JsonSuccess();
        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        public async Task<IActionResult> Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            return this.JsonSuccess(model);
        }


        /// <summary>
        /// 导出
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("Export")]
        [PermissionFilter(nameof(ZB_TrainPlanController) + "_" + nameof(ZB_TrainPlanController.Export), AuthorizationConst.EXPORT)]
        public async Task<IActionResult>
            Export(PagingModel<ZB_TrainPlanDto>
                pagingQueryModel)
        {
            var fileName = await _service.Export(pagingQueryModel);
            if (string.IsNullOrWhiteSpace(fileName))
                return this.JsonFaild(NULL_DATA_FOR_EXCEL_EXPORT);
            return this.JsonSuccess(fileName);
        }


        /// <summary>
        /// 完成年度计划培训_自动统一调用
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("WCPLANSTATE")]
        [PermissionFilter(nameof(ZB_TrainPlanController) + "_" + nameof(ZB_TrainPlanController.WCPLANSTATE), AuthorizationConst.SAVE)]
        public IActionResult WCPLANSTATE()
        {
            string result = _service.WCPLANSTATE();


            return this.JsonSuccess();

        }

        /// <summary>
        /// 查询_我的课程
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("WDKSGetList")]
        [PermissionFilter(nameof(ZB_TrainPlanController) + "_" + nameof(ZB_TrainPlanController.WDKSGetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> WDKSGetList(PagingModel<ZB_TrainPlanDto> pagingQueryModel)
        {
            IPageModel<ZB_TrainPlanDto> result;
            result = await _service.WDKSGetList(pagingQueryModel, UserIdentity.Id);
            return this.JsonSuccess(result);
        }


        #endregion

        #region  考试接口

        /// <summary>
        /// 参加考试
        ///</summary>
        /// <param name="id"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpGet("cjks")]
        [PermissionFilter(nameof(ZB_TrainPlanController) + "_" + nameof(ZB_TrainPlanController.cjks), "参加考试")]
        public async Task<IActionResult> cjks(Guid id, String type)
        {
            //id为ZB_ExamUser总部考试人员行id
            var result = await _service.cjks(id, type);
            if (result == null)
                return this.JsonFaild("您已经参加了5次考试，无法继续参加考试！", null);
            else
                return this.JsonSuccess(result);

        }


        /// <summary>
        /// 切换题目
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("lastornextAsync")]
        [PermissionFilter(nameof(ZB_TrainPlanController) + "_" + nameof(ZB_TrainPlanController.lastornextAsync), "切换题目")]
        public async Task<IActionResult> lastornextAsync(ZB_ExamQuestionsDto dto)
        {
            
            await _service.lastornext(dto);

            return this.JsonSuccess();

        }

        /// <summary>
        /// 提交试卷
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("tjsj")]
        [PermissionFilter(nameof(ZB_TrainPlanController) + "_" + nameof(ZB_TrainPlanController.tjsj), "提交试卷")]
        public async Task<IActionResult> tjsj(ZB_ExamQuestionsDto dto)
        {
            Guid result = await _service.tjsj(dto);


            return this.JsonSuccess(result);




        }


        #endregion


    }
}
